package com.zxht.wuchechenyun.activity;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zxht.wuchechenyun.R;
import com.zxht.wuchechenyun.fragment.FindCarFragment;
import com.zxht.wuchechenyun.fragment.IndexFragment;
import com.zxht.wuchechenyun.fragment.UserFragment;
import com.zxht.wuchechenyun.fragment.WaybillFragment;

public class MainActivity extends BaseActivity {

    private RadioGroup main_radio;
    private FragmentManager fragmentManager;
    private TextView title_name;

    public static MainActivity instance = null; // 记录页面状态

    public static boolean isForeground = false;


    @Override
    public int getLayoutResID() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

        instance = this;
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        main_radio = (RadioGroup) findViewById(R.id.main_radio);
//        fl_content = (FrameLayout) findViewById(R.id.fl_content);
        fragmentManager = getSupportFragmentManager();
        title_name = (TextView) findViewById(R.id.title_name);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }


    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }

    @Override
    public void initListener() {
        // radioButton装填改变监听
        main_radio.setOnCheckedChangeListener(new OnMyCheckedChangeListener());
        main_radio.check(R.id.rb_index);
    }

    @Override
    public void onClick(View v) {

    }

    private final class OnMyCheckedChangeListener implements
            RadioGroup.OnCheckedChangeListener {

        private IndexFragment indexFragment;
        private UserFragment userFragment;
        private WaybillFragment waybillFragment;
        private FindCarFragment findCarFragment;

        public void onCheckedChanged(RadioGroup group, int checkedId) {


            switch (checkedId) {
                // 首页
                case R.id.rb_index:
                    showFragment(1);
                    title_name.setText(getResources().getString(R.string.tab_index));
                    break;
                //找车源
                case R.id.rb_find_car:
                    showFragment(2);
                    title_name.setText(getResources().getString(R.string.tab_find_car));
                    break;
                //运单
                case R.id.rb_waybill:
                    showFragment(3);
                    title_name.setText(getResources().getString(R.string.tab_waybill));
                    break;
                // 我的
                case R.id.rb_user:
                    showFragment(4);
                    title_name.setText(getResources().getString(R.string.tab_user));
                    break;
                default:
                    break;
            }

        }

        public void showFragment(int index) {
            FragmentTransaction ft = fragmentManager.beginTransaction();

            // 想要显示一个fragment,先隐藏所有fragment，防止重叠
            hideFragments(ft);

            switch (index) {
                case 1:

                    // 如果homeFragment已经存在则将其显示出来
                    if (indexFragment != null)
                        ft.show(indexFragment);
                        // 否则是第一次切换则添加homeFragment，注意添加后是会显示出来的，replace方法也是先remove后add
                    else {
                        indexFragment = new IndexFragment();
                        ft.add(R.id.fl_content, indexFragment);
                    }
                    break;
                case 2:

                    if (findCarFragment != null)
                        ft.show(findCarFragment);
                    else {
                        findCarFragment = new FindCarFragment();
                        ft.add(R.id.fl_content, findCarFragment);
                    }
                    break;
                case 3:
                    if (waybillFragment != null)
                        ft.show(waybillFragment);
                    else {
                        waybillFragment = new WaybillFragment();
                        ft.add(R.id.fl_content, waybillFragment);
                    }

                    break;
                case 4:
                    if (userFragment != null)
                        ft.show(userFragment);
                    else {
                        userFragment = new UserFragment();
                        ft.add(R.id.fl_content, userFragment);
                    }
                    break;
            }
            ft.commit();
        }

        // 当fragment已被实例化，就隐藏起来
        public void hideFragments(FragmentTransaction ft) {
            if (indexFragment != null)
                ft.hide(indexFragment);
            if (findCarFragment != null)
                ft.hide(findCarFragment);

            if (waybillFragment != null)
                ft.hide(waybillFragment);
            if (userFragment != null)
                ft.hide(userFragment);
        }

    }

    private long exitTime = 0;
//    private FrameLayout fl_content;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            toast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
